package com.wad.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.wad.common.exception.BussinessException;
import com.wad.entity.product.Category;
import com.wad.manager.listener.ExcelListener;
import com.wad.manager.mapper.CategoryMapper;
import com.wad.manager.service.CategoryService;
import com.wad.vo.common.ResultCodeEnum;
import com.wad.vo.product.CategoryExcelVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wad
 * @date 2023/12/05 14:22
 * @project e-commerce
 **/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;


    @Override
    public List<Category> findCategoryList(Long id) {
        //根据id进行查询,得到每一层数据
        List<Category> categoryList=categoryMapper.selectCategoryList(id);
        //遍历返回的list集合判断每个分类是否有下一个分类，如果有设置hasChildren值为true
        if (CollectionUtil.isNotEmpty(categoryList)){
            for (Category category : categoryList) {
                int count=categoryMapper.selectCountByParentId(category.getId());
                if (count!=0){
//                    设置hasCildren为true
                    category.setHasChildren(true);
                }else {
                    category.setHasChildren(false);
                }
            }
        }
        return categoryList;
    }


    @Override
    public void exportData(HttpServletResponse response) {
        try {

            //设置响应信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            //这里设置URLEncoder.encode可以防止中文乱码，和easyexcel没关系
            String fileName = URLEncoder.encode("分类数据", "UTF-8");
            response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
            //调用mapper的方法查询所有分类
            List<Category> categoryList=categoryMapper.findAll();

            //将集合类型进行转换
            List<CategoryExcelVo> categoryExcelVos=new ArrayList<>();
            for (Category category : categoryList) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category,categoryExcelVo);
                categoryExcelVos.add(categoryExcelVo);
            }
            //调用easyexcel写的方法完成写的操作
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class)
                    .sheet("分类数据").doWrite(categoryExcelVos);
        }catch (Exception e){
            e.printStackTrace();
            throw new BussinessException(ResultCodeEnum.DATA_ERROR);
        }

    }


    @Override
    public void importData(MultipartFile file) {
        try {
            ExcelListener<CategoryExcelVo> excelListener=new ExcelListener<>(categoryMapper);
            EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,excelListener).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new BussinessException(ResultCodeEnum.DATA_ERROR);
        }

    }
}
